import Router from "koa-router"

import multer from "multer"

import UserController from "../controller/user.controller.js"
const { code, register, login, changePassword, saveUserInfo } = UserController
import { validator, verifyCode } from "../middleware/common.middleware.js"
import {
  verifyRegister,
  cryptPassword,
  verifyLogin,
  auth,
  verifyChangePassword,
} from "../middleware/user.middleware.js"

const router = new Router({ prefix: "/user" })
const upload = multer()
router.post("/profile",upload.single('avatar'), (ctx) => {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
  ctx.body = {
    aaa: 11111111,
  }
})

// 验证码
router.get("/code", code)

// 注册
router.post(
  "/register",
  validator({
    code: { type: "string", min: 4, max: 6, required: true },
    user_name: { type: "email", max: 32, required: true },
    password: { type: "string", min: 6, max: 64, required: true },
  }),
  verifyCode,
  verifyRegister,
  cryptPassword,
  register
)

// 登录
// router.post("/login", validator({
//   code: { type: "string", min: 4, max: 6, required: true },
//   user_name: { type: "email", max: 32, required: true },
//   password: { type: "string", min: 6, max: 64, required: true }
// }), verifyCode, verifyLogin, login)

router.post("/login", login)

// 修改密码
router.patch(
  "/",
  auth,
  validator({
    oldPassword: { type: "string", min: 6, max: 64, required: true },
    password: { type: "string", min: 6, max: 64, required: true },
  }),
  verifyChangePassword,
  cryptPassword,
  changePassword
)

// 保存用户资料
router.post("/saveUserInfo", saveUserInfo)

export default router
